package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.Blog;
import com.kun.index.entity.Message;
import com.kun.index.mapper.MessageMapper;
import com.kun.index.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消息通知表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-03-21
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Integer addMessage(Message message) {
        return messageMapper.insert(message);
    }

    @Override
    public Map<String, Object> queryMessages(HashMap<String, Object> map) {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
//        按照条件查询
        if (map.get("type")!=null){
            String type= (String) map.get("type");
            wrapper.eq("type",type);
        }
        if (map.get("userId")!=null){
            Integer userId= (Integer) map.get("userId");
            wrapper.eq("userId",userId);
        }
        if ( map.get("orderByCreateTime")!=null && (((String) map.get("orderByCreateTime")).equals("desc"))){
            wrapper.orderByDesc("createTime");
        }
//        分页查询
        if (map.get("pageIndex")!=null && map.get("pageSize")!=null){
            int pageIndex = (Integer) map.get("pageIndex");
            int pageSize = (Integer) map.get("pageSize");

            Page<Message> page = new Page<>(pageIndex,pageSize);
            messageMapper.selectPage(page, wrapper);
            long total =  page.getTotal();
            List<Message> messages = page.getRecords();
            resultMap.put("totalMessageNum",total);
            resultMap.put("messages",messages);
        }else {
            List<Message> messages = messageMapper.selectList(wrapper);
            resultMap.put("messages",messages);
        }
        return resultMap;
    }

    @Override
    public int updateMessage(Message message) {
        return messageMapper.updateById(message);
    }
}
